package com.central.apps.handler.MaterialInventory;

import cn.hutool.core.map.MapUtil;
import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.app.common.util.BLuntil;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 领料单
 */
@Component
public class lldHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {

        boolean flag = op.isFlag();//判断是否审核通过
        List<Map<String, Object>> list = handlerService.findBySql("select * from bl_lld where id="+id);
        String lldnum = MapUtil.getStr(list.get(0), "LLDNUM");//领料单编号
        String LOCATION = MapUtil.getStr(list.get(0), "LOCATION");//仓库编码
        String wonum = BLuntil.getParameter(list.get(0), "wonum");//工单编号
        List<Map<String, Object>> lineList = handlerService.findBySql("select *from bl_LLDLINE where LLDNUM ='"+lldnum+"'");
        if (lineList != null) {
            for (int k = 0; k < lineList.size(); k++) {
                String itemNUm= MapUtil.getStr(lineList.get(k), "ITEMNUM");//物资编码
                Integer qty= MapUtil.getInt(lineList.get(k), "ITEMQTY");//领料数量
                List<Map<String, Object>> inventorysList = handlerService.findBySql("select * from bl_Inventorys  where itemnum='"+itemNUm+"' and warehousenum ='"+LOCATION+"'");
                 if(inventorysList!=null && inventorysList.size()>0){
                    Integer currentqty= 0;//可用库存
                    Integer innum= 0;//预留数量
                    Integer actqty= 0;//实际库存
                    if(flowNode.getId().equals("fqr")){
                        if(flag) {
                            // 发起人提交修改预留数量及可用库存
                            currentqty= MapUtil.getInt(inventorysList.get(0), "currentqty")-qty;//可用库存
                            innum= MapUtil.getInt(inventorysList.get(0), "innum") + qty;//预留数量
                            // 更新库存数量
                            handlerService.findBySql("update  bl_Inventorys  set currentqty="+currentqty+" ,innum="+innum+" where itemnum='"+itemNUm+"' and warehousenum ='"+LOCATION+"'");
                            // 将领料单编号回写至工单计划物料中
                            handlerService.findBySql(" update bl_wpmaterial set lldnum='"+lldnum+"' where wonum='"+wonum+"' and itemnum='"+itemNUm+"'");
                        }else{
                            // 驳回释放预留及可用
                            currentqty= MapUtil.getInt(inventorysList.get(0), "currentqty")+qty;//可用库存
                            innum= MapUtil.getInt(inventorysList.get(0), "innum") - qty;//预留数量
                            // 更新库存数量
                            handlerService.findBySql("update  bl_Inventorys  set currentqty="+currentqty+" ,innum="+innum+" where itemnum='"+itemNUm+"' and warehousenum ='"+LOCATION+"'");
                            // 将工单计划物料中的领料单置空
                            handlerService.findBySql(" update bl_wpmaterial set lldnum='' where wonum='"+wonum+"' and itemnum='"+itemNUm+"'");

                        }
                    }
//                    if(flowNode.getId().equals("sh")){
//                        if(flag) {
//                            // 审核通过变更实际库存 释放预留
//                            innum= MapUtil.getInt(lineList.get(k), "innum") - qty;//预留数量
//                            actqty= MapUtil.getInt(lineList.get(k), "actqty") - qty;//实际库存数量
//                            handlerService.findBySql("update  bl_Inventorys  set actqty="+actqty+" ,innum="+innum+" where itemnum='"+itemNUm+"' and warehousenum ='"+LOCATION+"'");
//
//                        }
//                    }
                }
            }
        }

    }

}
